package com.gcxy.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户实体类
 *
 * @author pep7chiao
 * @date 2024/7/6
 */
@Data
@TableName("user")
@ApiModel("用户对象")
public class User implements UserDetails, Serializable {

    /**
     * 主键
     */
    @TableId(type = IdType.AUTO)
    @ApiModelProperty("ID")
    private Integer id;

    /**
     * 用户名
     */
    @ApiModelProperty("用户名")
    private String username;

    /**
     * 用户密码
     */
    @ApiModelProperty("用户密码")
    private String password;

    /**
     * 用户姓名
     */
    @ApiModelProperty("用户姓名")
    private String fullName;

    /**
     * 岗位编号
     */
    @ApiModelProperty("岗位编号")
    private Integer positionId;

    /**
     * 班级编号
     */
    @ApiModelProperty("班级编号")
    private Integer classId;

    /**
     * 组织机构编号
     */
    @ApiModelProperty("组织机构编号")
    private Integer agencyId;

    /**
     * 用户签名
     */
    @ApiModelProperty("用户签名")
    private String signature;


    @ApiModelProperty("用户备注")
    private String remarks;

    /**
     * 用户是否启用
     */
    @ApiModelProperty("用户是否启用")
    @Getter(AccessLevel.NONE)
    private Boolean enabled;

    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @ApiModelProperty("更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    /**
     * 更新者
     */
    @ApiModelProperty("更新者")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String updater;

    /**
     * 用户具有的角色
     */
    @ApiModelProperty(value = "角色")
    @TableField(exist = false)
    private List<com.gcxy.entity.Role> roles;

    @ApiModelProperty(value = "资源信息")
    @TableField(exist = false)
    private List<Resource> resourceList;


    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles.stream()
                .map(role -> new SimpleGrantedAuthority(role.getRoleName()))
                .collect(Collectors.toList());
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
